home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 014 / amiga3d / init.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  9KB  |  353 lines

  1. #include <exec/types.h>
  2. #include <exec/nodes.h>
  3. #include <exec/lists.h>
  4. #include <exec/memory.h>
  5. #include <hardware/blit.h>
  6. #include <hardware/custom.h>
  7. #include <graphics/gfx.h>
  8. #include <graphics/clip.h>
  9. #include <graphics/rastport.h>
  10. #include <graphics/view.h>
  11. #include <graphics/text.h>
  12. #include <graphics/gfxmacros.h>
  13.  
  14. #include <graphics/layers.h>
  15. #include <intuition/intuition.h>
  16. #include "threed.h"
  17.  
  18. /* #define DEBUG */
  19. /* #define ODEBUG */
  20. /* #define DEBUGDRAW */
  21. /* #define TICKDEBUG */
  22. /* #define TICKPRINT */
  23. #define FIXEDILLUMINATION
  24. #define CYCLECOLORS
  25.  
  26. #ifndef FIXEDILLUMINATION
  27.  
  28. extern UWORD colorpalette[];
  29.  
  30. #else
  31.  
  32. extern UWORD colorpalette[];
  33.  
  34. #endif
  35.  
  36. extern UBYTE title[];
  37.  
  38. extern struct Custom custom;
  39.  
  40. extern struct TmpRas tmpras;
  41.  
  42. extern struct BitMap bitmap0;
  43. extern struct BitMap bitmap1;
  44.  
  45. extern struct RastPort r[];
  46. extern struct RastPort *rp[];
  47.  
  48. extern struct RasInfo ri[];
  49. extern struct RasInfo *rip[];
  50.  
  51. extern struct RasInfo *irip;
  52.  
  53. extern BOOL initobject;
  54.  
  55. extern WORD pcount;
  56. extern WORD vcount;
  57.  
  58. extern UWORD frametoggle;
  59.  
  60. extern long GfxBase;
  61.  
  62. /******************************************************************************/
  63.  
  64. positioninit(c)
  65. struct Coordinate *c;
  66. {
  67.     c->x = 0;
  68.     c->y = 0;
  69.     c->z = 0;
  70. }
  71.  
  72. matrixinit(um)
  73. struct UV *um;
  74. {
  75.     um->uv11 = 0x4000;
  76.     um->uv12 = 0;
  77.     um->uv13 = 0;
  78.     um->uv21 = 0;
  79.     um->uv22 = 0x4000;
  80.     um->uv23 = 0;
  81.     um->uv31 = 0;
  82.     um->uv32 = 0;
  83.     um->uv33 = 0x4000;
  84. }
  85.  
  86. objectinit(view,screen,window,objectinfo,object)
  87. struct View *view;
  88. struct Screen *screen;
  89. struct Window *window;
  90. struct Objectinfo *objectinfo;
  91. struct Object *object;
  92. {
  93.    WORD *nextcolor;
  94.    struct Coordinate **nextn;
  95.    struct Coordinate **nextp;
  96.    int error = FALSE;
  97.  
  98.    /* initialize */
  99.  
  100.    objectinfo->subobjectinfo = NULL;
  101.    objectinfo->objectmatrix = object->umatrix;
  102.    objectinfo->objectposition = object->position;
  103.    objectinfo->objectnumpoints = object->pointcount;
  104.    objectinfo->objectpoints = object->pointstart;
  105.    objectinfo->objectnumnormals = object->normalcount;
  106.    objectinfo->objectnormals = object->normalstart;
  107.    objectinfo->objectnumpolys = object->polycount;
  108.    objectinfo->objectpolys = object->polystart;
  109.    objectinfo->objectprocedure = object->procedure;
  110.  
  111.     if ((objectinfo->displaymatrix= (struct UV *)AllocMem(sizeof(struct UV), MEMF_PUBLIC|MEMF_CLEAR)) == NULL)
  112.    {
  113. #ifdef DEBUG
  114.        printf("initobject: can't allocate objectinfo->displaymatrix...\n");
  115. #endif
  116.        return(FALSE);
  117.    }
  118.  
  119.     if ((objectinfo->displayposition= (struct UV *)AllocMem(sizeof(struct Coordinate), MEMF_PUBLIC|MEMF_CLEAR)) == NULL)
  120.    {
  121. #ifdef DEBUG
  122.        printf("initobject: can't allocate objectinfo->displayposition...\n");
  123. #endif
  124.        return(FALSE);
  125.    }
  126.  
  127.    if (objectinfo->objectnumpoints)
  128.  
  129.    if ((objectinfo->objectbufpoints = (struct Coordinate *)AllocMem(objectinfo->objectnumpoints * sizeof(struct Coordinate), MEMF_PUBLIC|MEMF_CLEAR)) == NULL)
  130.    {
  131. #ifdef DEBUG
  132.        printf("initobject: can't allocate objectinfo->objectbufpoints...\n");
  133. #endif
  134.        objectinfo->objectbufpointsize = 0;
  135.        return(FALSE);
  136.    }
  137.    else
  138.    {
  139.        objectinfo->objectbufpointsize = objectinfo->objectnumpoints * sizeof(struct Coordinate);
  140.    }
  141.  
  142.    if (objectinfo->objectnumnormals)
  143.  
  144.    if ((objectinfo->objectbufnormals = (struct Coordinate *)AllocMem(objectinfo->objectnumnormals * sizeof(struct Coordinate), MEMF_PUBLIC|MEMF_CLEAR)) == NULL)
  145.    {
  146. #ifdef DEBUG
  147.        printf("initobject: can't allocate objectinfo->objectbufnormals ...\n");
  148. #endif
  149.        objectinfo->objectbufnormalsize = 0;
  150.        return(FALSE);
  151.    }
  152.    else
  153.    {
  154.        objectinfo->objectbufnormalsize = objectinfo->objectnumnormals * sizeof(struct Coordinate);
  155.    }
  156.  
  157.    /* traverse the polygon list and initialize buffers for color, poff lists */
  158.  
  159.    for(pcount = 0; pcount < objectinfo->objectnumpolys; pcount++)
  160.    {
  161.        struct Polygon **np;
  162.  
  163.        np = (objectinfo->objectpolys+pcount);
  164.  
  165.        vcount += (*np)->vertexcount;
  166.    }
  167.  
  168.    if( (objectinfo->pptrbuf = (APTR)AllocMem((sizeof(APTR)*(vcount+1)),MEMF_PUBLIC|MEMF_CLEAR)) == NULL)
  169.    {
  170. #ifdef DEBUGDRAW
  171.        printf("draw: can't allocate objectinfo->pptrbuf...\n");
  172. #endif
  173.          objectinfo->pptrbufsize = 0;
  174.        return(FALSE);
  175.    }
  176.    else
  177.    {
  178.          objectinfo->pptrbufsize = sizeof(APTR)*(vcount+1);
  179.    }
  180.  
  181.    if( (objectinfo->nptrbuf = (APTR)AllocMem((sizeof(APTR)*(objectinfo->objectnumpolys+1)),MEMF_PUBLIC|MEMF_CLEAR)) == NULL)
  182.    {
  183. #ifdef DEBUGDRAW
  184.        printf("draw: can't allocate objectinfo->nptrbuf...\n");
  185. #endif
  186.          objectinfo->nptrbufsize = 0;
  187.        return(FALSE);
  188.    }
  189.    else
  190.    {
  191.          objectinfo->nptrbufsize = sizeof(APTR)*(objectinfo->objectnumpolys+1);
  192.    }
  193.  
  194.    if( (objectinfo->colorbuf = (APTR)AllocMem((sizeof(WORD)*(objectinfo->objectnumpolys+1)),MEMF_PUBLIC|MEMF_CLEAR)) == NULL)
  195.    {
  196. #ifdef DEBUGDRAW
  197.        printf("draw: can't allocate objectinfo->colorbuf...\n");
  198. #endif
  199.          objectinfo->colorbufsize = 0;
  200.        return(FALSE);
  201.    }
  202.    else
  203.    {
  204.          objectinfo->colorbufsize = sizeof(WORD)*(objectinfo->objectnumpolys+1);
  205.    }
  206.  
  207.  
  208.    /* intialize buffer pointers */
  209.  
  210.    nextcolor = objectinfo->colorbuf;
  211.    nextn = objectinfo->nptrbuf;
  212.    nextp = objectinfo->pptrbuf;
  213.  
  214.    for(pcount = 0; pcount < objectinfo->objectnumpolys; pcount++)
  215.    {
  216.        struct Polygon **np;
  217.        WORD vc;
  218.        struct Coordinate **v;
  219.        struct Coordinate *n;
  220.        long noff;
  221.        WORD color;
  222.  
  223. #ifdef DEBUG
  224.        printf("poly %lx: \n",pcount);
  225. #endif
  226.        np = (objectinfo->objectpolys+pcount);
  227.  
  228. #ifdef DEBUG
  229.        printf("np = %lx\n",np);
  230. #endif
  231.        v = (*np)->vertexstart;
  232.        n = (*np)->normalvector;
  233.        noff = n - (*(objectinfo->objectnormals));
  234.        color = (*np)->polycolor;
  235.  
  236. #ifdef DEBUG
  237.        printf("v = %lx\n", v);
  238.        printf("vertexcount = %lx\n",(*np)->vertexcount);
  239.        printf("n = %lx\n", n);
  240. #endif
  241.  
  242.        *nextcolor++ = color;
  243.        *nextn++ = (struct Coordinate *)(objectinfo->objectbufnormals+noff);
  244.  
  245. #ifdef DEBUGDRAW
  246.        printf("poly %lx: color = %lx\n",pcount,color);
  247. #endif
  248.  
  249. #ifdef DEBUGDRAW
  250.        printf("(objectinfo->objectbufnormals+noff)->x = %lx\n",(objectinfo->objectbufnormals+color)->x);
  251.        printf("(objectinfo->objectbufnormals+noff)->y = %lx\n",(objectinfo->objectbufnormals+color)->y);
  252.        printf("(objectinfo->objectbufnormals+noff)->z = %lx\n",(objectinfo->objectbufnormals+color)->z);
  253. #endif
  254.  
  255.             for(vc = 0; vc < (*np)->vertexcount; vc++)
  256.        {
  257.       long poff;
  258. #ifdef DEBUG
  259.       printf("v = %lx\n", v);
  260.       printf("vc = %lx\n",vc);
  261.       printf("v+vc = %lx\n",v+vc);
  262.       printf("(*(v+vc)) = %lx\n",(*(v+vc)));
  263. #endif
  264.       poff = (*(v+vc)) - (*(objectinfo->objectpoints));
  265.       /* poff = (long)(*(v+vc)); */
  266.       *nextp++ = (struct Coordinate *)(objectinfo->objectbufpoints+poff);
  267.  
  268. #ifdef DEBUGDRAW
  269.       printf("vertex %lx : poff = %lx\n",vc,poff);
  270. #endif
  271.  
  272. #ifdef DEBUGDRAW
  273.       printf("(objectinfo->objectbufpoints+poff)->x = %lx\n",(objectinfo->objectbufpoints+poff)->x);
  274.       printf("(objectinfo->objectbufpoints+poff)->y = %lx\n",(objectinfo->objectbufpoints+poff)->y);
  275.       printf("(objectinfo->objectbufpoints+poff)->z = %lx\n",(objectinfo->objectbufpoints+poff)->z);
  276. #endif
  277.        }
  278.  
  279.    }
  280.  
  281.    /* terminate pointer buffer arrays with a null pointer */
  282.  
  283.    *nextcolor = 0;   
  284.    *nextn = 0;   
  285.    *nextp = 0;   
  286.  
  287.    /* allocate subobjectinfo structures dependent on this object */
  288.  
  289.    {
  290.        struct Object *ob;
  291.  
  292.        /* for all the objects in this objectsegment */
  293.  
  294.        for (ob = object->subobject ; ob; ob = ob->nextobject)
  295.        {
  296.       struct Objectinfo *thisubobjectinfo;
  297.  
  298. #ifdef ODEBUG
  299.       printf("objectinit: allocate objectinfo for subobject(%lx) ",ob);
  300. #endif
  301.  
  302.       /* allocate an objectinfo structure for the current object */
  303.  
  304.       if ((thisubobjectinfo = (struct Objectinfo *)AllocMem(sizeof(struct Objectinfo),MEMF_PUBLIC|MEMF_CLEAR)) == NULL) 
  305.       {
  306.           return(error); 
  307.       }
  308.  
  309. #ifdef ODEBUG
  310.       printf("= %lx\n",thisubobjectinfo);
  311. #endif
  312.       /* initialize the buffers for the current 3d subobject */
  313.  
  314.       if(!objectinit(view,screen,window,thisubobjectinfo,ob))
  315.       {
  316.          return(error);
  317.       }
  318.  
  319.       /* make this objectinfo last on the subobjectinfo list */
  320.       {
  321.           struct Objectinfo **soipp;
  322.  
  323.           soipp =  &objectinfo->subobjectinfo;
  324.           while (*soipp)
  325.           {
  326.          soipp = &((*soipp)->nextobjectinfo);
  327.           }
  328.           *soipp = thisubobjectinfo;
  329.            thisubobjectinfo->nextobjectinfo = NULL;
  330.       }
  331.  
  332.        }
  333.  
  334.    }
  335.  
  336. #ifdef ODEBUG
  337.     printf("objectinit: &objectinfo->subobjectinfo = %lx\n",&objectinfo->subobjectinfo );
  338.     {
  339.    struct Objectinfo *soip;
  340.  
  341.    printf("    SUBOBJECTINFO LIST     \n");
  342.    printf("_________________________\n");
  343.  
  344.    for (soip = objectinfo->subobjectinfo; soip; soip = soip->nextobjectinfo)
  345.           printf("    subobjectinfo(%lx)\n",soip);
  346.  
  347.    printf("_________________________\n");
  348.     }
  349. #endif
  350.  
  351.         return(TRUE);
  352. }
  353.